﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace _24._02.CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.sqlConnection1.ConnectionString =
                "Integrated Security=SSPI;" +
                "Persist Security Info=False;" +
                "User ID='';" +
                "Initial Catalog=NorthWind;" +
                "Data Source=HP;" +
                "Initial File Name=''";
        }

        private void btnInserisciRecord_Click(object sender, EventArgs e)
        {
            string SQL = "";

            // Lettura del valore più alto di ID
            SQL = "SELECT TOP 1 ID " +
                  "FROM Clienti " +
                  "ORDER BY ID DESC";
            this.sqlConnection1.Open();
            SqlCommand Comando = new SqlCommand(
                SQL, this.sqlConnection1);
            int ID = Convert.ToInt32(Comando.ExecuteScalar());
            this.sqlConnection1.Close();

            // Definizione nuovo ID
            ID += 1;

            // Scrittura di un nuovo record
            SQL = "INSERT INTO " +
                  "[NorthWind].[dbo].[Clienti] " +
                  "([ID], " +
                  "[Nome], " +
                  "[Cognome], " +
                  "[Indirizzo], " +
                  "[Città]) " +
                "VALUES(" + ID.ToString() + ", " +
                  "'Giuseppe', " +
                  "'Verdi', " +
                  "'Via Rossi, 17', " +
                  "'ROMA')";
            this.sqlConnection1.Open();
            Comando = new SqlCommand(SQL, this.sqlConnection1);
            int Numero = Comando.ExecuteNonQuery();
            this.sqlConnection1.Close();
            this.Label1.Text = "Inserimento eseguito!";
        }
    }
}
